package com.mk.Controller;


import com.mk.Entity.*;
import com.mk.Service.IBlogService;
import com.mk.Service.ITagService;
import com.mk.Service.ITypeService;
import com.mk.Service.IUserservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @author 28464
 */

@RequestMapping("/admin")
@Controller
public class AdminController {

    @Autowired
    IUserservice userservice;

    @Autowired
    IBlogService blogService;

    @Autowired
    ITypeService typeService;

    @Autowired
    ITagService tagService;


    /**
     * 这里是登录的相关操作
     * @return
     */
    //到登陆页面
    @GetMapping("/login")
    public String getLogin(){
        return "admin/login";
    }

//登陆验证，并到首页
    @PostMapping("/login")
    public String login(User user){
        if(user!=null){
            String username=user.getUsername();
            String paswword=user.getPassword();

            User user1=userservice.queryUser(username);
            if(paswword.equals(user1.getPassword())){
                return "admin/index";
            }
        }
        return "admin/login";
    }


    /**
     * 这里是博客的相关操作
     * @return
     */

    //转到博客列表页面
    @GetMapping("/blogList")
    public ModelAndView getBloglist(){
        List<Type> typeList=typeService.queryAll();
        ModelAndView mv=new ModelAndView();
        mv.addObject("typelist",typeList);
        mv.setViewName("admin/blog-list");
        return mv;
    }


    //查询所有的博客
    @GetMapping("/listBlog")
    public String listBlog(queryObject queryObject ,Model model){
        List<Blog> blist=blogService.queryAll(queryObject);
        Integer totalCount=blogService.queryCount(queryObject);
        PageResult<Blog> pageResult=new PageResult<>(queryObject.getCurrentPage(),totalCount,5,blist);
        JsonResult jsonResult=new JsonResult("查询博客成功!",true,pageResult);
        model.addAttribute("jsonResult", jsonResult);
        System.out.println(jsonResult);
//        System.out.println("list:"+blist);
//        System.out.println(totalCount);
//        System.out.println(queryObject);
        return "admin/blog-list::bloglist";
    }


    //新增和修改的功能
    @PostMapping(value = "/inOrupBlog")
    public String inOrupBlog(Blog blog,Model model){
        if(blog.getId()==null){
            //新增
            blog.setCreateDate(new Date());
            blog.setViews(0);
            //将博客对应的type的count加一
            Type type=typeService.queryOne(blog.getType().getId());
            type.setCount(type.getCount()+1);
            typeService.updateType(type);
            //
            blog.setType(type);
            blog.setTags(tagService.queryTagString(blog.getTagids()));
            blogService.insertBlog(blog);
            JsonResult jsonResult=new JsonResult("新增成功",true,null);
            model.addAttribute("jsonResult", jsonResult);
            return "redirect:/admin/blogList";
        }else {
            //更新
            blogService.updateBlog(blog);
            JsonResult jsonResult=new JsonResult("更新成功",true,null);
            model.addAttribute("jsonResult", jsonResult);
            return "redirect:/admin/blogList";
        }
    }

//    跳转到新增页
    @GetMapping(value = {"/blogInput/{bid}","/blogInput"})
    public ModelAndView getBlogInput(@PathVariable(required = false) Long bid){
        ModelAndView mv=new ModelAndView();
        if(bid!=null){
            Blog blog=blogService.queryOne(bid);
            mv.addObject("blog",blog);
        }
        else {
            //在th:object中的blog不能为空，所以得new一个blog
            Blog blog=new Blog();
            mv.addObject("blog",blog);
        }

        List<Type> types=typeService.queryAll();
        List<Tag> tags=tagService.queryAll();

        mv.addObject("types",types);
        mv.addObject("tags",tags);
        mv.setViewName("admin/blog-input");
        return mv;
    }



    //删除博客
    @PostMapping("/deleteBlog/{bid}")
    public String deleteBlog(@PathVariable("bid") Long bid, Model model){
        blogService.deleteBlog(bid);
        return "redirect:/admin/blogList";
    }


    /**
     * 这里是分类的相关操作
     * @return
     */


    //到分类页面
    @GetMapping("/type")
    public String getType(){
        return "admin/type";
    }

    @GetMapping("/listType")
    public String listType(queryObject queryObject,Model model){
        Integer count=typeService.queryCount();
        List<Type> typeList = typeService.queryAllLimit(queryObject);
        PageResult<Type> pageResult=new PageResult<>(queryObject.getCurrentPage(),count,queryObject.getPageSize(),typeList);
        JsonResult jsonResult=new JsonResult("查询成功",true,pageResult);
        model.addAttribute("jsonResult",jsonResult);
        return "admin/type::typelist";
    }

    @ResponseBody
    @PostMapping("/deleteType")
    public JsonResult deleteType(Long id){
        typeService.deleteType(id);
        JsonResult jsonResult=new JsonResult("删除成功",true,null);
        return jsonResult;
    }


    @ResponseBody
    @PostMapping("/upOrinType")
    public JsonResult upOrinType(Type type){
        if(type.getId()==null){
            //新增
            typeService.insertType(type);
            JsonResult jsonResult=new JsonResult("新增成功",true,null);
            return jsonResult;
        }else{
            //更新
            typeService.updateType(type);
            typeService.insertType(type);
            JsonResult jsonResult=new JsonResult("新增成功",true,null);
            return jsonResult;
        }
    }


    /**
     * 这里是标签的操作
     * @return
     */

    //转到标签
    @GetMapping("/tag")
    public String getTag(){
        return "admin/tag";
    }

    //查询tag 分页
    @GetMapping("/listTag")
    public String tagList(Model model,queryObject queryObject){
        Integer count = tagService.queryCount();
        List<Tag> tagList = tagService.queryAllLimit(queryObject);
        PageResult<Tag> pageResult=new PageResult<>(queryObject.getCurrentPage(),count, queryObject.getPageSize(), tagList);
        System.out.println("taglist"+tagList);
        JsonResult jsonResult=new JsonResult("查询成功",true,pageResult);
        model.addAttribute("jsonResult",jsonResult);
        return "admin/tag :: taglist";
    }
    //删除tag
    @ResponseBody
    @PostMapping("/deleteTag")
    public JsonResult deleteTag(Long id){
        tagService.deleteTag(id);
        JsonResult jsonResult=new JsonResult("删除成功",true,null);
        return jsonResult;
    }

    //新增和更新tag
    @ResponseBody
    @PostMapping("/upOrinTag")
    public JsonResult upOrinTag(Tag tag){
        if(tag.getId()==null){
            //新增
            tagService.insertTag(tag);
            JsonResult jsonResult=new JsonResult("新增成功",true,null);
            return jsonResult;
        }else {
            //更新
            tagService.updateTag(tag);
            JsonResult jsonResult=new JsonResult("更新成功",true,null);
            return jsonResult;
        }
    }
}
